package com.dx.mes.service.tool.service;

import com.dx.common.table.ppt.Ret_tool_st_rln;
import com.dx.common.table.ppt.Ret_tool_st_rlnId;
import com.dx.mes.common.dao.IRetToolStRlnDao;
import com.dx.mes.common.transcoder.util.*;
import com.dx.mes.facade.tool.service.IFtptolrelateService;
import com.dx.mes.facade.tool.tx.Ftptolrelate.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;

import static com.dx.mes.common.bean.def.GenericDef.*;

/**
 * Created by ZhongXu on 2017/7/10.
 */
@Service("ftptolrelateService")
public class FtptolrelateService implements IFtptolrelateService {
    private LogUtils logUtils;
    private static String TX_NAME = T_FTPTOLRELA;


    @Autowired
    private IRetToolStRlnDao retToolStRlnDao;

    @Override
    public String subMainProc(String evt_no, String strInMsg) {
        AppContext.clear();
        AppContext.setCurrServiceName(FtptolopeService.class.getSimpleName());
        AppContext.setCurrEventNumber(evt_no);
        logUtils = new LogUtils(FtptolopeService.class);
        logUtils.info("[InTrx:" + strInMsg + "]");
        String outMsg = null;
        FtptolrelateO outTrx = new FtptolrelateO();
        outTrx.setTrx_id(TX_NAME);
        outTrx.setType_id(TRX_OUT);
        outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));

        try {
            FtptolrelateI inTrx = JacksonUtil.fromJson(strInMsg, FtptolrelateI.class);
            char action_flg = inTrx.getAction_flg().charAt(0);
            switch (action_flg) {
                case ACTION_FLG_QUERY:
                    queryByCondition(inTrx,outTrx);
                    break;
                case ACTION_FLG_ADD:
                    addFnc(inTrx,outTrx);
                    break;
                case ACTION_FLG_UPDATE:
                    updateFnc(inTrx,outTrx);
                    break;
                case ACTION_FLG_DELETE:
                    cancelFnc(inTrx,outTrx);
                    break;
                case 'F':
                    updateListFnc(inTrx,outTrx);
                    break;
                default:
                    errActionFnc(outTrx);
            }
            if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
               // retMaskToolDao.flushCurSession();
            } else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }

        } catch (Exception ex) {
            outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
            outTrx.setRtn_mesg(ex.toString());
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        } finally {
            outMsg = JacksonUtil.toJSONStr(outTrx);
            logUtils.info("[OutTrx:" + outMsg + "]");
        }
        return outMsg;
    }
    public void queryByCondition(FtptolrelateI inTrx, FtptolrelateO outTrx){
        FtptolrelateIA iary = inTrx.getIary().get(0);
        Integer  rlnLvl  = iary.getRln_lvl();
        String  sourToolIdFk = iary.getSour_tool_id_fk();
        String  destToolIdFk = iary.getDest_tool_id_fk();
        String  rlnStat = iary.getRln_stat();
        String hql = "FROM Ret_tool_st_rln where 1=1";
        if (null != rlnLvl && !"".equals(rlnLvl)) {
            hql += " and rln_lvl = '"+rlnLvl+"'";
        }
        if (null != sourToolIdFk && !"".equals(sourToolIdFk)) {
            hql += " and sour_tool_id_fk = '"+sourToolIdFk+"'";
        }
        if (null != destToolIdFk && !"".equals(destToolIdFk)) {
            hql += " and dest_tool_id_fk = '"+destToolIdFk+"'";
        }
        if (null != rlnStat && !"".equals(rlnStat)) {
            hql += " and rln_stat = '"+rlnStat+"'";
        }
        List<Ret_tool_st_rln> ret_tool_st_rlns = retToolStRlnDao.find(hql);
        List<FtptolrelateOA>  oaryList = new ArrayList<FtptolrelateOA>();
        for(Ret_tool_st_rln ret_tool_st_rln :ret_tool_st_rlns){
            FtptolrelateOA  oary  = new FtptolrelateOA();
            oary.setSour_tool_id_fk(ret_tool_st_rln.getId().getSour_tool_id_fk());
            oary.setDest_tool_id_fk(ret_tool_st_rln.getId().getDest_tool_id_fk());
            oary.setRln_stat(ret_tool_st_rln.getId().getRln_stat());
            oary.setUnq_seq_id(ret_tool_st_rln.getUnq_seq_id());
            oary.setRln_lvl(ret_tool_st_rln.getRln_lvl());
            oary.setStat_prty(ret_tool_st_rln.getStat_prty());
            oary.setEvt_usr(ret_tool_st_rln.getEvt_usr());
            if(ret_tool_st_rln.getEvt_timestamp()!=null){
                oary.setEvt_timestamp(ret_tool_st_rln.getEvt_timestamp().toString());
            }
            oary.setEvt_note(ret_tool_st_rln.getEvt_note());
            oaryList.add(oary);
        }
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
        outTrx.setOary(oaryList);
        outTrx.setTbl_cnt(oaryList.size());
    }
    public void  addFnc(FtptolrelateI inTrx, FtptolrelateO outTrx) {
        String  evt_usr = inTrx.getEvt_usr();
        FtptolrelateIA iary = inTrx.getIary().get(0);
        Integer  rlnLvl  = iary.getRln_lvl();
        String  sourToolIdFk = iary.getSour_tool_id_fk();
        String  destToolIdFk = iary.getDest_tool_id_fk();
        String  rlnStat = iary.getRln_stat();
        String  evtNote  =  iary.getEvt_note();
        Ret_tool_st_rlnId id = new Ret_tool_st_rlnId();
        id.setDest_tool_id_fk(destToolIdFk);
        id.setRln_stat(rlnStat);
        id.setSour_tool_id_fk(sourToolIdFk);
        Ret_tool_st_rln ret_tool_st_rlns = retToolStRlnDao.get(id);
        if ( ret_tool_st_rlns != null) {
            outTrx.setRtn_code(E_FTPTOLRELA_TOOL_EXSIT + "");//定义
            outTrx.setRtn_mesg("机台关系已经存在");
            return;
        }
        String hql = "FROM Ret_tool_st_rln where 1=1";
        if (null != destToolIdFk && !"".equals(destToolIdFk)) {
            hql += " and dest_tool_id_fk = '"+destToolIdFk+"'";
        }
        if (null !=rlnStat  && !"".equals(rlnStat)) {
            hql += " and rln_stat = '"+rlnStat+"'";
        }
        List<Ret_tool_st_rln> ret_tool_st_rlnInfo = retToolStRlnDao.find(hql);
        Ret_tool_st_rln ret_tool_st_rln = new Ret_tool_st_rln();
        if("RUN".equals(rlnStat)){
            ret_tool_st_rln.setStat_prty(1);
        }else if("IDLE".equals(rlnStat)){
            ret_tool_st_rln.setStat_prty(2);
        }else{
            ret_tool_st_rln.setStat_prty(3);
        }
        ret_tool_st_rln.setId(id);
        ret_tool_st_rln.setUnq_seq_id(GUIDGenerator.javaGUID());
        ret_tool_st_rln.setEvt_timestamp(Util.getCurrentTimestamp());
        ret_tool_st_rln.setEvt_usr(evt_usr);
        ret_tool_st_rln.setRln_lvl(rlnLvl );
        ret_tool_st_rln.setEvt_note(evtNote);
        ret_tool_st_rln.setEvt_cate(EVT_FTPTOLRELA_ADD);
        retToolStRlnDao.save(ret_tool_st_rln);
        List<FtptolrelateOA>  oaryList = new ArrayList<FtptolrelateOA>();
        FtptolrelateOA  oary  = new FtptolrelateOA();
        oary.setSour_tool_id_fk(ret_tool_st_rln.getId().getSour_tool_id_fk());
        oary.setDest_tool_id_fk(ret_tool_st_rln.getId().getDest_tool_id_fk());
        oary.setRln_stat(ret_tool_st_rln.getId().getRln_stat());
        oary.setUnq_seq_id(ret_tool_st_rln.getUnq_seq_id());
        oary.setRln_lvl(ret_tool_st_rln.getRln_lvl());
        oary.setStat_prty(ret_tool_st_rln.getStat_prty());
        oary.setEvt_timestamp(ret_tool_st_rln.getEvt_timestamp().toString());
        oary.setEvt_usr(ret_tool_st_rln.getEvt_usr());
        oaryList.add(oary);
        outTrx.setOary(oaryList);
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
    }
    public void  updateFnc(FtptolrelateI inTrx, FtptolrelateO outTrx) {
        String  evt_usr = inTrx.getEvt_usr();
        FtptolrelateIA iary = inTrx.getIary().get(0);
        Integer  rlnLvl  = iary.getRln_lvl();
        String  sourToolIdFk = iary.getSour_tool_id_fk();
        String  destToolIdFk = iary.getDest_tool_id_fk();
        String  rlnStat = iary.getRln_stat();
        String  evtNote  =  iary.getEvt_note();
        Ret_tool_st_rlnId id = new Ret_tool_st_rlnId();
        id.setDest_tool_id_fk(destToolIdFk );
        id.setRln_stat(rlnStat);
        id.setSour_tool_id_fk(sourToolIdFk);
        Ret_tool_st_rln ret_tool_st_rln = retToolStRlnDao.get(id);
        if ( ret_tool_st_rln == null) {
            outTrx.setRtn_code(E_FTPTOLRELA_TOOL_NOEXSIT + "");
            outTrx.setRtn_mesg("要修改机台关系不存在");
            return;
        }
        ret_tool_st_rln.setEvt_timestamp(Util.getCurrentTimestamp());
        ret_tool_st_rln.setEvt_usr(evt_usr);
        ret_tool_st_rln.setEvt_note(evtNote);
        ret_tool_st_rln.setEvt_cate(EVT_FTPTOLRELA_UPDATE);
        retToolStRlnDao.update(ret_tool_st_rln);
        List<FtptolrelateOA>  oaryList = new ArrayList<FtptolrelateOA>();
        FtptolrelateOA  oary  = new FtptolrelateOA();
        oary.setSour_tool_id_fk(ret_tool_st_rln.getId().getSour_tool_id_fk());
        oary.setDest_tool_id_fk(ret_tool_st_rln.getId().getDest_tool_id_fk());
        oary.setRln_stat(ret_tool_st_rln.getId().getRln_stat());
        oary.setUnq_seq_id(ret_tool_st_rln.getUnq_seq_id());
        oary.setRln_lvl(ret_tool_st_rln.getRln_lvl());
        oary.setStat_prty(ret_tool_st_rln.getStat_prty());
        oary.setEvt_note(ret_tool_st_rln.getEvt_note());
        oaryList.add(oary);
        outTrx.setOary(oaryList);
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
    }
    public void  cancelFnc(FtptolrelateI inTrx, FtptolrelateO outTrx) {
        FtptolrelateIA iary = inTrx.getIary().get(0);
        String  sourToolIdFk = iary.getSour_tool_id_fk();
        String  destToolIdFk = iary.getDest_tool_id_fk();
        String  rlnStat = iary.getRln_stat();
        Ret_tool_st_rlnId id = new Ret_tool_st_rlnId();
        id.setDest_tool_id_fk(destToolIdFk );
        id.setRln_stat(rlnStat);
        id.setSour_tool_id_fk(sourToolIdFk);
        Ret_tool_st_rln ret_tool_st_rlns = retToolStRlnDao.get(id);
        if ( ret_tool_st_rlns == null) {
            outTrx.setRtn_code(E_FTPTOLRELA_TOOL_NOEXSIT + "");
            outTrx.setRtn_mesg("要删除机台关系不存在");
            return;
        }
        retToolStRlnDao.delete(ret_tool_st_rlns);
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
    }
    public void  updateListFnc(FtptolrelateI inTrx, FtptolrelateO outTrx) {
        FtptolrelateIA iary = inTrx.getIary().get(0);
        String  evt_usr = inTrx.getEvt_usr();
        List<FtptolrelateIB>  list=iary.getFtptolrelateIBs();
        for (FtptolrelateIB ftptolrelateIB :list){
            Ret_tool_st_rlnId id = new Ret_tool_st_rlnId();
            id.setDest_tool_id_fk(ftptolrelateIB.getDest_tool_id_fk());
            id.setRln_stat(ftptolrelateIB.getRln_stat());
            id.setSour_tool_id_fk(ftptolrelateIB.getSour_tool_id_fk());
            Ret_tool_st_rln ret_tool_st_rlns = retToolStRlnDao.get(id);
            if ( ret_tool_st_rlns != null) {
                outTrx.setRtn_code(E_FTPTOLRELA_TOOL_EXSIT + "");//定义
                outTrx.setRtn_mesg("机台关系已经存在");
                return;
            }
            Ret_tool_st_rln ret_tool_st_rln = new Ret_tool_st_rln();
            if("RUN".equals(ftptolrelateIB.getRln_stat())){
                ret_tool_st_rln.setStat_prty(1);
            }else if("DOWN".equals(ftptolrelateIB.getRln_stat())){
                ret_tool_st_rln.setStat_prty(2);
            }else{
                ret_tool_st_rln.setStat_prty(3);
            }
            ret_tool_st_rln.setId(id);
            ret_tool_st_rln.setUnq_seq_id(GUIDGenerator.javaGUID());
            ret_tool_st_rln.setEvt_timestamp(Util.getCurrentTimestamp());
            ret_tool_st_rln.setEvt_usr(evt_usr);
            ret_tool_st_rln.setRln_lvl(ftptolrelateIB.getRln_lvl());
            ret_tool_st_rln.setEvt_note(ftptolrelateIB.getEvt_note());
            ret_tool_st_rln.setEvt_cate(EVT_FTPTOLRELA_ADD);
            retToolStRlnDao.save(ret_tool_st_rln);
        }
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
    }
    public void errActionFnc(FtptolrelateO outTrx) {
        outTrx.setRtn_code("0000009");
        outTrx.setRtn_mesg("Invalid Action_flg");
    }
}
